package com.zxmr.hospital.info.action;

import java.util.Map;

import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.account.Sex;
import com.zxmr.domain.account.User;
import com.zxmr.domain.service.UserService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;

public class EditPersonalInfo extends Executor{

	@Override
	public void execute(Request request, Response response) {
		
		
		User requestUser = (User)request.getAccount();
		try {
			updateUser(request,requestUser);
			sendACK(response);
		} catch (ExecuteException e) {
			sendFailACK(response,e.getMessage());
			return;
		}
	}
	private void sendFailACK(Response response,String message)
	{
		ResponsePayload payload = new ResponsePayload(message);
		response.setPayload(payload);
		response.setResult(ResultCode.FAILED);
	}
	private void sendACK(Response response)
	{
		ResponsePayload payload = new ResponsePayload("success");
		response.setPayload(payload);
	}
	private void updateUser(Request request,User user)throws ExecuteException
	{
		Map<String,String> parameters = request.getRequestParameters();

		updateSex(parameters.get("sex"),user);
		updatePassword(parameters.get("password"),user);
		updateEmail(parameters.get("email"),user);
		updateDisplayName(parameters.get("display"),user);
		directUpdate(parameters,user);
		
		UserService service = new UserService();
		service.updateUser(user);
		
		String action = ResourceManager.getInstance().getMessage("com.zxmr.info.updatePersonal");
		UserTraceManager.addTrace(action, request.getAccount());
	}
	private void directUpdate(Map<String,String> parameters,User user)
	{
		user.setCell(parameters.get("cell"));
		user.setAge(Integer.parseInt(parameters.get("age")));
		user.setCollege(parameters.get("college"));
		user.setEducation(parameters.get("education"));
	}
	private void updateDisplayName(String display,User user)throws ExecuteException
	{
		if(display == null || "".equals(display.trim()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.personal.nodisplay"));
		user.setDisplay(display);
	}
	private void updatePassword(String password,User user)throws ExecuteException
	{
		if(password == null || "".equals(password.trim()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.personal.nopassword"));
		
		user.setPassword(password);
	}
	private void updateEmail(String email,User user)throws ExecuteException
	{
		if(email == null || "".equals(email.trim()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.personal.noemail"));
		if(email.equals(user.getEmail()))return;
		
		UserService service = new UserService();
		User u = service.findByEmail(email);
		if(u != null)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.personal.email.exist"));
		
		user.setEmail(email);
	}
	private void updateSex(String sex,User user)throws ExecuteException
	{
		try
		{
			Sex sexe = Enum.valueOf(Sex.class, sex);
			user.setSex(sexe);
		}catch(IllegalArgumentException iae)
		{
			throw new ExecuteException("sex should be MAN or WOMAN");
		}catch(NullPointerException npe)
		{
			throw new ExecuteException("sex should be MAN or WOMAN");
		}
	}
}
